const App = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    searchValue: '',
    goodslist: [],
    navData: [],
    typeid: 0,
    activeKey: 0,
    rightArr: [],
    selectId: 'item0',
    leftHeight: 0,
    leftId: 'left0'
  },
  search() {
    wx.navigateTo({
      url: '/pages/cakeMall/search/search',
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

    console.log("hhaa", options);
    if (options.typeid) {
      this.setData({
        typeid: Number(options.typeid)
      })

    }
    this.gettype();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    let Height = 0;
    wx.getSystemInfo({
      success: (res) => {
        Height = res.windowHeight
      }
    })
    const query = wx.createSelectorQuery();
    query.selectAll('.search').boundingClientRect()
    query.exec((res) => {
      this.setData({
        leftHeight: (Height - res[0][0].height) * 2
      })
    })
  },


  //计算右侧每个锚点的高度
  selectHeight() {
    let h = 0;
    const query = wx.createSelectorQuery();
    query.selectAll('.item').boundingClientRect()
    query.exec((res) => {
      let right = res[0].map((item) => {
        h += item.height
        return h
      })
      this.setData({
        rightArr: right,
      })
    })

  },

  //监听scroll-view的滚动事件
  watchScroll(e) {
    let scrollTop = e.detail.scrollTop; //获取距离顶部的距离
    let {activeKey, rightArr} = this.data;
    if (activeKey + 1 < rightArr.length && scrollTop >= rightArr[activeKey]) {
      this.setData({
        activeKey: activeKey + 1,
        leftId: `left${activeKey+1}`
      })
    }
    if (activeKey - 1 >= 0 && scrollTop < rightArr[activeKey - 1]) {
      this.setData({
        activeKey: activeKey - 1,
        leftId: `left${activeKey-1}`
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  gettype: function () {
    let _this = this;
    App._post_form('wxapi/category/lists', {
      type: 2
    }, function (result) {
      if (result.status == "200") {
        console.log("sssss", result.data);
        var list = result.data;
        let arr = [];
        list.forEach(item => {
          arr.push({
            id: item.id,
            name: item.name,
            list: []
          })
        })
        _this.setData({
          navData: arr,
          activeKey: 0
        });
        if (_this.data.typeid) {
          let arrdata = _this.data.navData;
          console.log('有数据', arrdata.indexOf(arrdata.find(item => item.id === _this.data.typeid)))
          _this.setData({
            activeKey: arrdata.indexOf(arrdata.find(item => item.id === _this.data.typeid))
          })
        }
        if (result.data.length > 0) {
          _this.getdata();
        }
      }

    });
  },
  getdata() {
    let _this = this;
    var keys = this.data.searchValue;

    App._post_form('wxapi/goods/lists', {
      page: 1,
      rows: 999,
      category_id: ''
    }, function (result) {
      console.log("ss", result.data);
      let typedata = _this.data.navData;
      typedata.forEach((key, index) => {
        key.list = result.data.filter(item => item.category_id === key.id);
      })
      _this.setData({
        goodslist: typedata
      })


      _this.selectHeight()

      if (_this.data.typeid) {
        let arrdata = _this.data.navData;
        _this.setData({
          selectId: `item${arrdata.indexOf(arrdata.find(item => item.id === _this.data.typeid))}`
        })
      }

    });

  },
  onChange(event) {
    let index = event.currentTarget.dataset.index
    this.setData({
      activeKey: index,
      selectId: "item" + index
    });
  },

})